import os
def writes(f,dicts):
    f.write('# Initial process that execs /init.\n')
    f.write('# This code runs in user space.\n')
    f.write('\n#include "include/sysnum.h"\n')
    f.write('# exec(init, argv)\n')
    f.write('.global start\n')
    f.write('start:\n')
    for k in dicts:
        f.write('\tla a0,'+k +'\n')
        f.write('\tla a1,argv\n')
        f.write('\tli a7,SYS_exec\n')
        f.write('\tecall\n')
    f.write('# for(;;) exit()\n')
    f.write('end:\n')
    f.write('\tli a7,SYS_exit\n')
    f.write('\tecall\n')
    f.write('\tjal end\n')
    for k in dicts:
        f.write(k + ':\n')
        f.write('\t.string \"/'+k+'\\0\"\n')
    f.write('.p2align 2\n')
    f.write('argv:\n')
    f.write('\t.long 0\n')

f = open('./user/runall.S','r+',encoding='utf-8')
dirPath = './sd'
dicts = []
taboo = ['sh','init','.DS_Store','mnt']
f.truncate(0)
for name in os.listdir(dirPath):
    if os.path.isdir(name):
        continue
    elif name in taboo:
    	continue
    else:
        dicts.append(name)
writes(f,dicts)
